var gLoading=false;
var gPage=2;
var tpl = $('script[type="text/template"]').html(); 
$(function(){
	$("#test2").unbind();
	$("#test2").click(function(event) {
		CLOFOOD.gourl(gChooseUrl,'choose');
	});
	$(".searchicon").click(function(event) {
		CLOFOOD.gourl(gSearchUrl,'search');
	});
	$(".categoryicon").click(function(event) {
		window.location=gCategoryUrl;
	});

	var format={
		sem_name:function(data){
			return SubSemname(data.sem_name,48);
		}
	}

	$(window).scroll(function(){
		if ($(document).height() - $(this).scrollTop() - $(this).height()<100 && !gLoading) {
			gLoading = true;
			$.post(gRxspUrl,{
			  'page':gPage++
			},function(data){
				if(data.status==1 && data.data.length>0){
					var html=Pagetpl(tpl,data.data,format);
					$(".rmsc").append(html);
					$(".lazy").each(function(){
						if($(this).hasClass("img-error1")){
							$(this).lazyload({
						 	effect : "fadeIn"
			     			});
						}
					});
					gLoading = false;
			   }
			   else{
			   		$('.zl').hide(500);
			   }
			},'json');
			return false;
		}
    });

    $(".wrap").on('click', '.productdetail', function(event) {
    	event.preventDefault();
    	// if(IsBuycartTrigger(event)){
    	// 	return true;
    	// }
    	var product_no=$(this).data("product_no");
    	var url=gDetailUrl+"&product_no="+product_no;
    	CLOFOOD.gourl(url,"productDetail",{product_no:product_no,productNo:product_no})
    });

    //添加购物车
    $('.btn-buy').unbind();
	$('.wrap').on('click','.btn-buy',function(event){
		event.stopPropagation();
		var obj=this;
		var product_no=$(obj).parents('li').data('product_no');
		if(!CLOFOOD.addbuycart(product_no,1,'',function(){
			//掉落效果
			if(!CLOFOOD.isAppType()){
				var img = $(obj).parent().find('img');
				var flyElm = img.clone().css('opacity', 0.75);
				$('body').append(flyElm);
				flyElm.css({
					'z-index': 9000,
					'display': 'block',
					'position': 'absolute',
					'top': img.offset().top +'px',
					'left': img.offset().left +'px',
					'width': img.width() +'px',
					'height': img.height() +'px'
				});
				flyElm.animate({
					top: $('.head-shopcart').offset().top,
					left: $('.head-shopcart').offset().left,
					width: 20,
					height: 32
				}, 'slow', function() {
					flyElm.remove();
				});
			}
			//底部购物车+1
			var count=$(".footer").find('.quant').html();
			$(".footer").find('.quant').html(count*1+1);
			CLOFOOD.apprun("buycartAdd");
		})){
			return false;
		}
		
	});

	//跳转的链接
	$(".jumpurl").click(function(event) {
		var url=$(this).data("url");
		if(url==''){
			return;
		}
		CLOFOOD.gourl(url,'openUrl');
	});
});

//跳转摇一摇
function yaoyiyao(){
	if(CLOFOOD.isAppType() && !CLOFOOD.islogin()){
		CLOFOOD.gourl('','toLogin');
		return;
	}
	CLOFOOD.gourl('index1.php?c=yaoyiyao&a=run','openUrl');
	
}
//跳转更多分类
function gocategorymore(id){
	CLOFOOD.gourl('index1.php?c=product&a=run&category='+id+'&children=1','productList',{category:id,children:1});
}





